
#ifndef __AUTO_LOCK_WRITE_H
#define __AUTO_LOCK_WRITE_H

#include "APRRWMutex.h"

/** An APRRWMutex automatic locking wrapper class for a read/write mutex.
 * Destroying the wrapped mutex afterwards is to be handled elsewhere. */
class APRAutoLockWrite
{
   public:
      /** Lock the read/write mutex (for writing, implying a writer is accessing
       * and updating the resource).
       * 
       * @param i_rwmutex
       * A pointer to a created APRRWMutex that is unlocked. */
      APRAutoLockWrite(APRRWMutex *i_rwmutex)
      {
         _rwmutex = i_rwmutex;
         _rwmutex->lockWrite();
      }
      /** Unlock the mutex (the writer has finished the operation on this
       * resource). */
      ~APRAutoLockWrite()
      {
         if (_rwmutex) _rwmutex->unlock();
      }
      /** This removes the lock and turns off the mutex before the destructor. */
      void turnOff()
      {
         _rwmutex->unlock();
         _rwmutex = NULL;
      }
   private:
      /** A pointer to the APR read/write mutex that is to be used. */
      APRRWMutex *_rwmutex;
};

#endif
